Estimated time needed: 45 minutes
After completing this lab, you will be able to:
!jupyter nbconvert --version
6.0.7
plotly.graph_objects: This is a low level interface to figures, traces and layout. The Plotly graph objects module provides an automatically generated hierarchy of classes ( figures, traces, and layout) called graph objects. These graph objects represent figures with a top-level class plotly.graph_objects.Figure.
plotly.express: Plotly express is a high-level wrapper for Plotly. It is a recommended starting point for creating the most common figures provided by Plotly using a simpler syntax. It uses graph objects internally. Now let us use these libraries to plot some charts We will start with plotly_graph_objects to plot line and scatter plots
Note: You can hover the mouse over the charts whenever you want to view any statistics in the visualization charts
# Import required libraries
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
A scatter plot shows the relationship between 2 variables on the x and y-axis. The data points here appear scattered when plotted on a two-dimensional plane. Using scatter plots, we can create exciting visualizations to express various relationships, such as:
##Example 1: Let us illustrate the income vs age of people in a scatter plot
age_array=np.random.randint(25,55,60)
# Define an array containing salesamount values
income_array=np.random.randint(300000,700000,3000000)
age_array
array([39, 44, 43, 49, 47, 25, 34, 34, 25, 50, 41, 38, 27, 47, 37, 38, 49,
39, 35, 36, 52, 41, 30, 37, 37, 49, 34, 44, 29, 52, 25, 32, 47, 46,
37, 41, 46, 46, 36, 27, 46, 40, 46, 42, 26, 37, 46, 45, 41, 46, 30,
41, 29, 31, 52, 50, 36, 50, 40, 44])
##First we will create an empty figure using go.Figure()
fig=go.Figure()
fig
#Next we will create a scatter plot by using the add_trace function and use the go.scatter() function within it
# In go.Scatter we define the x-axis data,y-axis data and define the mode as markers with color of the marker as blue
fig.add_trace(go.Scatter(x=age_array, y=income_array, mode='markers', marker=dict(color='blue')))